﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

using EShop.Core;
using EShop.Core.Caching;
using EShop.Core.Data;
using EShop.Core.Events;
using EShop.Core.Model.Store;

namespace EShop.Services.Store
{

    public class BrandService : IBrand
    {
        #region Constants
        private const string BRAND_BY_ID_KEY = "EShop.Brand.id-{0}";
        private const string BRAND_PATTERN_KEY = "Era.Brand";
        #endregion

        #region Fields
        private readonly IRepository<Brand> _brandRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        //private readonly IDbContext _context;
        #endregion

        #region Ctor
        public BrandService(IRepository<Brand> brandRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _brandRepository = brandRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Methods
        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns>
        /// 0.品牌添加失败
        /// 1.品牌添加成功
        /// 2.
        /// </returns>
        public int AddBrand(Brand model)
        {
            if (model == null) throw new ExtException("NUll Brand Model");
            //判断是后可以重复注册品牌
            if (_brandRepository.Table.Where(c => c.BrandName.ToLower() == model.BrandName.Trim().ToLower()).Count() > 0) return 3;
            ////判断品牌的Logo
            //if (_brandRepository.Table.Where(c => c.BrandLogo.ToLower() == model.BrandLogo.Trim().ToLower()).Count() > 0) return 2;

            int rs = _brandRepository.Insert(model);
            if (rs > 0)
            {
                rs = 1;
                //event notification
                _eventPublisher.EntityInserted(model);

            }
            return rs;
        }
        /// <summary>
        /// 修改品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateBrand(Brand model)
        {
            if (model == null) throw new ExtException("NUll Brand Model");

            int rs = _brandRepository.Update(model);
            if (rs > 0)
            {
                //event notification
                _eventPublisher.EntityUpdated(model);

            }
            return rs > 0 ? true : false;
        }
        /// <summary>
        /// delete brand
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelBrand(string id)
        {

            var model = _brandRepository.GetById(id);
            if (model == null)
                throw new ArgumentNullException("NUll Brand Model");

            int r= _brandRepository.Delete(model);

            if(r>0)
            {
            //cache
                _cacheManager.RemoveByPattern(BRAND_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(model);
            }
            return r > 0 ? true : false;
        }

        /// <summary>
        /// get all brand(deleted data not show)
        /// </summary>
        /// <param name="showHidden"></param>
        /// <returns></returns>
        public IList<Brand> GetAllBrand(bool showHidden=false)
        {
            var query = _brandRepository.Table;
            if (!showHidden)
                query = query.Where(c => c.Published);

            query = query.Where(c => !c.Deleted);
            query = query.OrderBy(c => c.Id);
            var unsortedBrand = query.ToList();

            //sort categories

            return unsortedBrand;
        }

        /// <summary>
        /// 品牌分类
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public IPagedList<Brand> GetAllBrand(int pageIndex, int pageSize, bool showHidden = false)
        {
            var Brand = GetAllBrand(showHidden);
            //filter
            return new PagedList<Brand>(Brand,pageIndex, pageSize);
        }
        /// <summary>
        ///获取一条品牌信息 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Brand GetBrandById(int id,bool showHidden)
        {    
            if (id <= 0)
                return null;
            //格式化缓存条件
            string key = string.Format(BRAND_BY_ID_KEY, id);
            return _cacheManager.Get(key, () =>
            {
                var brand = _brandRepository.GetById(id);
                return brand;
            });

        }
        #endregion
    }
}
